package com.huel.taobaostore;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.huel.bean.Goods;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ShowActivity extends Activity {
    private ImageView goods_iv;
    private TextView goods_price;
    private TextView goods_title;
    private Button addGoodsBtn;
    private ImageView backButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);
        goods_iv=findViewById(R.id.goods_iv);
        goods_price=findViewById(R.id.goods_price);
        goods_title=findViewById(R.id.goods_title);
        addGoodsBtn=findViewById(R.id.addGoodsBtn);
        backButton=findViewById(R.id.backButton);
        final Goods goods=(Goods)getIntent().getSerializableExtra("goods"); //取出商品信息 填充详情页
        Glide.with(this).load(goods.getPic()).into(goods_iv);
        goods_title.setText(goods.getName());
        goods_price.setText("促销价:"+goods.getPrice());

        addGoodsBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                queryDialog(goods);
            }
        });
        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳回到首页
                Intent intent = new Intent(ShowActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });
    }


    public void addGoods(Goods goods) {
        //请求添加商品到购物粗车 暂时写死 以后更新 再改
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url("http://192.168.199.229:8080/Android_war_exploded/cart?action=addItems&id=" + goods.getId() +
                        "&username=2580")
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                //失败回调
              runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        show("请检查网络");
                    }
                });
            }

            @Override
            public void onResponse(Call call, final Response response) throws IOException {
                //响应成功,这个回调在子线程中,所以不需要创建线程
                if (response.isSuccessful()) {
                    //isSuccessful方法：如果代码位于（200…300）中，则返回true，这意味着请求已成功接收
                    //在这写  更新UI操作
                   runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            show("添加购物车成功");
                        }
                    });
                }
            }
        });
    }

    public void queryDialog(final Goods goods) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View view01 = LayoutInflater.from(this).inflate(R.layout.activity_query_dialog, null);
        TextView cancel = view01.findViewById(R.id.no);
        TextView sure = view01.findViewById(R.id.yes);
        TextView context = view01.findViewById(R.id.query);
        context.setText("确定要将" + goods.getName() + "加入购物车吗？");
        final Dialog dialog = builder.create();
        dialog.show();
        dialog.getWindow().setContentView(view01);
        dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addGoods(goods);
                dialog.dismiss();
            }
        });
    }
    public void show(String information) {
        Toast.makeText(this, information, Toast.LENGTH_SHORT).show();
    }
}
